from django.shortcuts import render_to_response
from django.template import RequestContext
#from django.http import HttpResponseRedirect
from django.views.generic.list_detail import object_list
from lotto.const import GAME, ORDERS
from lotto.statistic.forms import ModeForm



def archive (request, mode):
    request.session['sort'] = request.GET.get ('sort', 'desc');
    if request.method == 'POST':
        form = ModeForm (request.POST);
        if form.is_valid ():
            request.session['mode'] = form.cleaned_data['mode'];
    else:
        form = ModeForm (initial = {'mode':mode})
        request.session['mode'] = mode;
    form.fields['mode'].widget.attrs = {'onchange':"this.form.submit();"};
    lotto = GAME[request.session['mode']].objects.all ().order_by (ORDERS[request.session['sort']]);
    return object_list (request, queryset = lotto,
                        template_name = 'catalog/archive.html',
                        paginate_by = 25,
                        template_object_name = 'lottos',
                        extra_context = {'form':form});


def current (request):
    lotto = [];
    for l in GAME:
        try:
            lotto.append (GAME[l].objects.latest ('id'));
        except:
            pass;
    if request.session.has_key ('mode'):
        del request.session['mode'];
    return render_to_response ('catalog/current.html',
							   {'lotto':lotto},
    						   context_instance = RequestContext (request));


def days (request):
    return render_to_response ('catalog/days.html',
							   context_instance = RequestContext (request));


def payment (request):
    return render_to_response ('catalog/payment.html',
							   context_instance = RequestContext (request));

def principle (request):
    return render_to_response ('catalog/principle.html',
							   context_instance = RequestContext (request));

